var t = getApp(),
    a = t.requirejs("core"),
    o = (t.requirejs("jquery"), t.requirejs("foxui"),
        t.requirejs("wxParse/wxParse")),
    e = 0;
Page({
    data: {
        goods_id: 0,
        advHeight: 1,
        isfavorite: false,
        isfavoriteid: 0,
        setIntervaltime: '',
        detailcolor: true
    },
    imageLoad: function (t) {
        var a = t.detail.height,
            o = t.detail.width,
            e = Math.floor(750 * a / o);
        a == o ? this.setData({
            advHeight: 750
        }) : this.setData({
            advHeight: e
        });
    },
    onLoad: function (e) {
        var i = this;
        t.getCache("isIpx") ? i.setData({
            isIpx: !0,
            iphonexnavbar: "fui-iphonex-navbar"
        }) : i.setData({
            isIpx: !1,
            iphonexnavbar: ""
        });
        //s 是 商品id
        var s = e.id;
        this.setData({
            goods_id: s
        }), a.post("groups.goods", {
            id: s,
            zczlstatus: 0
        }, function (t) {
            //t 获取众筹详情数据
            console.log(t, 2222);
            if (t.error == "达到人数上限") {
                wx.showToast({
                    title: "众筹已完成",
                    icon: "none"
                })
                setTimeout(() => {
                    wx.navigateBack({
                        delta: 0,
                    })
                }, 2000)
            } else {
                let gnum_pric = t.data.gnum_pric.toFixed(2)
                t.data.gnum_pric = gnum_pric
                let baifen = 100 / t.data.groupnum * t.data.fightnum
                baifen = baifen.toFixed(2)
                i.setData({
                    baifen: baifen
                })
                i.setData({
                    data: t.data
                }), o.wxParse("wxParseData", "html", t.data.content, i, "0");
            }

        });
        this.setData({
            // setIntervaltime: setInterval(() => {
            //     this.countDown(this.data.data.createtime, this.data.data.endtime)
            // }, 1000)
        })
        setTimeout(() => {
            this.countDown(this.data.data.createtime, this.data.data.endtime)
        }, 500);
    },
    Offered() {
        if (this.data.time[0] < 0) {
            wx.showToast({
                title: '众筹已结束',
                icon: 'none'
            })
        } else if (this.data.time[1] <= 0 && this.data.time[0] <= 0 && this.data.time[2] <= 0) {
            wx.showToast({
                title: '众筹已结束',
                icon: 'none'
            })
        } else {
            a.post("groups/goods/check_tuan", {
                id: this.data.data.tmid,
                type: "group",
                zczlstatusL: 0
            }, ((e) => {
                if (e.data.order_num != 0) {
                    wx.showToast({
                        title: '已参团',
                    })
                } else {
                    // a.post("groups.goods.goodsCheck", {
                    //     id: this.data.goods_id,
                    //     type: "group",
                    // }, ((e) => {
                    //     console.log(e);
                    // }))
                    wx.navigateTo({
                        url: "/other/crowdfundingconfirm/crowdfundingconfirm?type=groups&id=" + this.data.goods_id + "&heads=0" + "&teamid=" + this.data.data.tmid
                    })
                }
            }))
        }
    },
    favorite: function (t) {
        var e = this;
        a.get("member/favorite/toggle", {
            id: e.data.goods_id,
            isfavorite: t.currentTarget.dataset.isfavorite
        }, function (el) {
            e.setData({
                isfavorite: t.currentTarget.dataset.isfavorite == 1 ? true : false,
                isfavoriteid: t.currentTarget.dataset.isfavorite == 1 ? 1 : 0
            })
            if (t.currentTarget.dataset.isfavorite == 1) {
                wx.showToast({
                    title: '收藏成功',
                    icon: 'none'
                })
            } else if (t.currentTarget.dataset.isfavorite == 0) {
                wx.showToast({
                    title: '取消收藏',
                    icon: 'none'
                })
            }
        });
    },
    singlebuy: function (t) {
        var o = this;
        a.post("groups/goods/goodsCheck", {
            id: o.data.goods_id,
            type: "single"
        }, function (t) {
            if (1 != t.error)
                if (0 == o.data.data.more_spec) wx.navigateTo({
                    url: "../confirm/index?id=" + o.data.goods_id + "&type=single"
                });
                else {
                    o.setData({
                        layershow: !0,
                        options: !0
                    }), o.setData({
                        optionarr: [],
                        selectSpecsarr: []
                    });
                    var e = o.data.data.id;
                    a.get("groups.goods.get_spec", {
                        id: e
                    }, function (t) {
                        o.setData({
                            spec: t.data
                        });
                    }), o.setData({
                        layershow: !0,
                        options: !0
                    });
                }
            else a.alert(t.message);
        });
    },
    close: function () {
        this.setData({
            layershow: !1,
            options: !1
        });
    },
    countDown: function (t, a) {
        var o = parseInt(Date.now() / 1e3),
            s = parseInt((t > o ? t : a) - o),
            i = Math.floor(s / 86400),
            d = Math.floor((s - 24 * i * 60 * 60) / 3600),
            n = Math.floor((s - 24 * i * 60 * 60 - 3600 * d) / 60),
            r = Math.floor(s - 24 * i * 60 * 60 - 3600 * d - 60 * n);
        0 == i && 0 == d && 0 == n && 0 == r && this.get_details(this.data.teamid);
        var u = [i, d, n, r];
        this.setData({
            time: u
        });
    },
    specsTap: function (t) {
        e++;
        var o = this,
            i = o.data.spec,
            s = a.pdata(t).spedid,
            n = a.pdata(t).id,
            d = a.pdata(t).specindex;
        a.pdata(t).idx;
        i[d].item.forEach(function (t, a) {
            t.id == n ? i[d].item[a].status = "active" : i[d].item[a].status = "";
        }), o.setData({
            spec: i
        });
        var r = o.data.optionarr,
            p = o.data.selectSpecsarr;
        1 == e ? (r.push(n), p.push(s)) : p.indexOf(s) > -1 ? r.splice(d, 1, n) : (r.push(n),
            p.push(s)), o.data.optionarr = r, o.data.selectSpecsarr = p, a.post("groups.goods.get_option", {
            spec_id: o.data.optionarr,
            groups_goods_id: o.data.goods_id
        }, function (t) {
            o.setData({
                optiondata: t.data
            });
        });
    },
    buy: function (t) {
        var o = this,
            e = (a.pdata(t).op, o.data.goods_id),
            i = o.data.optiondata;
        o.data.optiondata ? i.stock > 0 ? wx.navigateTo({
            url: "../confirm/index?id=" + e + "&option_id=" + i.id + " &type=single",
            success: function () {
                o.setData({
                    layershow: !1,
                    chosenum: !1,
                    options: !1
                });
            }
        }) : wx.showToast({
            title: "库存不足",
            icon: "none",
            duration: 2e3
        }) : wx.showToast({
            title: "请选择规格",
            icon: "none",
            duration: 2e3
        });
    },
    onReady: function () {},
    onShow: function () {},
    onHide: function () {},
    onUnload: function () {
        clearInterval(this.data.datasetIntervaltime)
    },
    onPullDownRefresh: function () {},
    onReachBottom: function () {},
    onShareAppMessage: function () {
        // return {
        //     title: this.data.data.title
        // };
    },
    check: function () {}
});